<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="/include/commonHeader::commonHeader('代码类型新增')"></head>
<body>
<div class="wrapper wrapper-content">
    <div class="col-sm-12">
        <form class="form-horizontal m-t" id="exampleForm">
            <input id="parentCode" name="parentCode" type="hidden" th:value="${parentCode}">
            <input id="moduleFlag" name="moduleFlag" type="hidden" th:value="${moduleFlag}">
            <div class="form-group">
                <div class="col-xs-3 form-label-v">
                    <label><font style="color:red;">*</font>代码：</label>
                </div>
                <div class="col-xs-8">
                    <input id="cCode" name="cCode" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3 form-label-v">
                    <label><font style="color:red;">*</font>名称：</label>
                </div>
                <div class="col-xs-8">
                    <input id="cName" name="cName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3 form-label-v">
                    <label><font style="color:red;">*</font>顺序：</label>
                </div>
                <div class="col-xs-8">
                    <input id="idx" name="idx" class="form-control" type="number">
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3 form-label-v">
                    <label>备注：</label>
                </div>
                <div class="col-xs-8">
                    <input id="remark" name="remark" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <div class="text-center">
                    <button id="btn_save" onclick="save()" type="button" class="btn btn-primary">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="/plugins/bootstrapValidator/js/bootstrapValidator.min.js"></script>
<script src="/plugins/bootstrapValidator/js/language/zh_CN.js"></script>
<!-- 自定义js -->
<script type="text/javascript">
    var prefix = "/code";
    $(function () {
        initValidator();
    });


    //保存
    function save() {
        $("#exampleForm").data('bootstrapValidator').validate();//触发一次所有的验证
        if (!$("#exampleForm").data('bootstrapValidator').isValid()) {//判断验证是否已经通过
            return;
        }

        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/save.do",
            data: $('#exampleForm').serialize(),
            error : function(request) {
                toastr.error("Connection error");
            },
            success : function(data) {
                if (data.code == 0) {
                    parent.toastr.success("操作成功");
                    parent.reload();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                } else {
                    toastr.warning(data.msg)
                }
            }
        });
    }

    //初始化验证规则
    function initValidator() {
        $('#exampleForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                cCode: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '代码不能为空'
                        },
                        remote: {
                            delay: 1,
                            message: '该类型下已存在相同代码',
                            url: '/code/check.do',
                            data: {
                                type: 'cCode',
                                parentCode: $("#parentCode").val(),
                                moduleFlag: $("#moduleFlag").val()
                            }
                        }
                    }
                },
                cName: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '名称不能为空'
                        },
                        remote: {
                            delay: 1,
                            message: '该类型下已存在相同名称',
                            url: '/code/check.do',
                            data: {
                                type: 'cCode',
                                parentCode: $("#parentCode").val(),
                                moduleFlag: $("#moduleFlag").val()
                            }
                        }
                    }
                },
                idx: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '序号不能为空'
                        },
                        remote: {
                            delay: 1,
                            message: '该类型下已存在相同序号',
                            url: '/code/check.do',
                            data: {
                                type: 'cCode',
                                parentCode: $("#parentCode").val(),
                                moduleFlag: $("#moduleFlag").val()
                            }
                        }
                    }
                }
            }
        });
    }
</script>
</body>
</html>
